<script setup>
import { ref, onMounted } from 'vue';

const projects = ref([]);

const fetchProjects = async () => {
  try {
    // 替换为你的实际API端点
    const response = await fetch('http://8.134.145.163:8088/projects/all');
    const data = await response.json();
    projects.value = data;
  } catch (error) {
    console.error('获取项目数据失败:', error);
  }
};

// 组件挂载时获取数据
onMounted(() => {
  fetchProjects();
});

const toggleLike = (project) => {
  project.likes += 1;
};
</script>

<template>
  <div class="project-container">
    <div class="projects-grid">
      <div v-for="project in projects" 
           :key="project.id" 
           class="project-card">
        <div class="card-image">
          <img :src="project.imageUrl" :alt="project.title">
          <div class="tech-stack">
            <span v-for="tech in project.technologies" 
                  :key="tech" 
                  class="tech-tag">
              {{ tech }}
            </span>
          </div>
        </div>
        
        <div class="card-content">
          <h3 class="project-title">{{ project.title }}</h3>
          <p class="project-desc">{{ project.description }}</p>
          
          <div class="card-footer">
            <div class="action-buttons">
              <button class="like-btn" @click="toggleLike(project)">
                <i class="heart-icon">❤️</i>
                <span>{{ project.likes }}</span>
              </button>
              <a :href="project.demoUrl" 
                 target="_blank" 
                 class="demo-btn">
                预览
              </a>
              <a :href="project.githubUrl" 
                 target="_blank" 
                 class="github-btn">
                代码
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.project-container {
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
  overflow-x: hidden;
}

.projects-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  padding: 16px;
}

.project-card {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  transition: all 0.3s ease;
  height: 100%;
  display: flex;
  flex-direction: column;
  backdrop-filter: blur(8px);
}

.project-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

/* 修复图片容器和图片显示 */
.card-image {
  position: relative;
  width: 100%;
  height: 200px; /* 固定高度 */
  overflow: hidden;
}

.card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
}

.card-content {
  padding: 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.project-title {
  margin: 0 0 8px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.project-desc {
  margin: 0 0 12px 0;
  font-size: 13px;
  color: #666;
  line-height: 1.4;
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 3; /* 限制显示3行 */
  line-clamp: 3; /* 添加标准属性 */
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.tech-stack {
  position: absolute;
  top: 8px;
  left: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tech-tag {
  background: rgba(255, 255, 255, 0.9);
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 11px;
  color: #333;
  backdrop-filter: blur(4px);
}

.card-footer {
  border-top: 1px solid #eee;
  padding-top: 12px;
  margin-top: auto;
}

.action-buttons {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: flex-start;
}

.like-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  padding: 4px 8px;
  cursor: pointer;
  color: #666;
  font-size: 13px;
}

.demo-btn, .github-btn {
  padding: 4px 12px;
  border-radius: 16px;
  font-size: 13px;
  text-decoration: none;
  transition: all 0.3s ease;
}

.demo-btn {
  background: #ff2442;
  color: white;
}

.github-btn {
  background: #f5f5f5;
  color: #333;
}

.heart-icon {
  font-size: 14px;
}

/* 响应式布局 */
@media (max-width: 1400px) {
  .projects-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 1024px) {
  .projects-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .card-image {
    height: 180px;
  }
}

@media (max-width: 640px) {
  .projects-grid {
    grid-template-columns: 1fr;
  }
  
  .card-image {
    height: 200px;
  }
}
</style> 